###############################################################################
# Senators at Home 
# By Jaclyn Kaslovsky  
###############################################################################

# Packages ##############################################

require(readstata13)
require(lfe)
require(dplyr)
require(stargazer)
require(starpolishr)
require(dplyr)

##########################################################
#### Table 2: Predicting where senators go and staff ##### 
##########################################################

#load the data
data_county <- read.dta13("resources_tocounties_final.dta",convert.factors=FALSE)

data_msa <- read.dta13("resources_tomsa_final.dta",convert.factors=FALSE)

#visits
reg5_county <- felm(traveledonce ~ core + swing + greaterdon + logpop + median_hhincome + per_white + has_flight + seniority + chair + power + majority + electionyear + has_capital | year + icpsr | 0 | icpsr, data = data_county)

reg6_county <- felm(log_traveled ~ core + swing + greaterdon + logpop + median_hhincome + per_white + has_flight + seniority + chair + power + majority + electionyear + has_capital | year + icpsr | 0 | icpsr, data = data_county)

reg7_county <- felm(traveled ~ core + swing + greaterdon + logpop + median_hhincome + per_white + has_flight + seniority + chair + power + majority + electionyear + has_capital | year + icpsr | 0 | icpsr, data = data_county)

reg5_msa <- felm(traveledonce ~ core + swing + greaterdon + logpop + median_hhincome + per_white + has_flight + seniority + chair + power + majority + electionyear + has_capital | year + icpsr | 0 | icpsr, data = data_msa)

reg6_msa <- felm(log_traveled ~ core + swing + greaterdon + logpop + median_hhincome + per_white + has_flight + seniority + chair + power + majority + electionyear + has_capital | year + icpsr | 0 | icpsr, data = data_msa)

reg7_msa <- felm(traveled ~ core + swing + greaterdon + logpop + median_hhincome + per_white + has_flight + seniority + chair + power + majority + electionyear + has_capital | year + icpsr | 0 | icpsr, data = data_msa)

#staff
reg8_county <- felm(staffeddonce ~ core + swing + greaterdon + logpop + median_hhincome + per_white + has_flight + seniority + chair + power + majority + electionyear + has_capital | year + icpsr | 0 | icpsr, data = data_county)

reg9_county <- felm(log_staffed ~ core + swing + greaterdon + logpop + median_hhincome + per_white + has_flight + seniority + chair + power + majority + electionyear + has_capital | year + icpsr | 0 | icpsr, data = data_county)

reg10_county <- felm(staff_per ~ core + swing + greaterdon + logpop + median_hhincome + per_white + has_flight + seniority + chair + power + majority + electionyear + has_capital | year + icpsr | 0 | icpsr, data = data_county)

reg8_msa <- felm(staffeddonce_msa ~ core + swing + greaterdon + logpop + median_hhincome + per_white + has_flight + seniority + chair + power + majority + electionyear + has_capital | year + icpsr | 0 | icpsr, data = data_msa)

reg9_msa <- felm(log_staffed_msa ~ core + swing + greaterdon + logpop + median_hhincome + per_white + has_flight + seniority + chair + power + majority + electionyear + has_capital | year + icpsr | 0 | icpsr, data = data_msa)

reg10_msa <- felm(staff_per_msa ~ core + swing + greaterdon + logpop + median_hhincome + per_white + has_flight + seniority + chair + power + majority + electionyear + has_capital | year + icpsr | 0 | icpsr, data = data_msa)


county <- stargazer(reg5_county, reg7_county,reg6_county, reg8_county, reg10_county, reg9_county,
                    omit=c("seniority", "chair", "power", "majority", "has_flight", "electionyear", "has_capital"),
                    add.lines=list(c("Senator-County Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark"),
                                   c("Year Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark")),
                    notes.append = FALSE, notes.label = "",
                    report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                    omit.stat = c("rsq", "ser","f"), font.size = "footnotesize",
                    dep.var.caption  = "",
                    dep.var.labels = c("", "", "","", "", ""), model.numbers = FALSE,
                    column.labels=c("Any Visits","Total Visits","ln(Visits + 1)","Any Staff", "Pct. Staff","ln(Pct. of Staff + 1)"), align=TRUE,
                    covariate.labels=c("Core Area", "Swing Area", "Above Avg. Donations to Party", "Log Population", "Median Household Income", "Percent White"),
                    notes="\\parbox[t]{\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with standard errors (clustered on senator) shown in parentheses. The dependent variables are various transformations of the number of resources a senator allocates to an area. $^*$ indicates $p<0.10$ and $^{**} p<0.05$ (two tailed tests).}",
                    label="tripscountiesmember",
                    digits=3,
                    digits.extra = 0,column.sep.width = "-8pt",
                    title="The Relationship between Local Characteristics and Senator Resource Allocation")

msa <- stargazer(reg5_msa, reg7_msa,reg6_msa, reg8_msa, reg10_msa, reg9_msa,
                 omit=c("seniority", "chair", "power", "majority", "has_flight", "electionyear", "has_capital"),
                 add.lines=list(c("Senator-MSA Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark"),
                                c("Year Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark")),
                 notes.append = FALSE, notes.label = "",
                 report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                 omit.stat = c("rsq", "ser","f"), font.size = "footnotesize",
                 dep.var.caption  = "",
                 dep.var.labels = c("", "", "","", "", ""), model.numbers = FALSE,
                 column.labels=c("Any Visits","Total Visits","ln(Visits + 1)","Any Staff", "Pct. Staff","ln(Pct. of Staff + 1)"), align=TRUE,
                 covariate.labels=c("Core Area", "Swing Area", "Above Avg. Donations to Party", "Log Population", "Avg. Median Household Income", "Percent White"),
                 notes="",
                 label="tripscountiesmember",
                 digits=3,
                 digits.extra = 0,column.sep.width = "-8pt",
                 title="")

star.panel.out <- star_panel(county, msa,panel.label.fontface="bold",
                             panel.names = c("County", "Metropolitan Statistical Area"), same.summary.stats = FALSE)
cat(star.panel.out, sep = '\n', file = "Table2.tex")

#descriptive stats
#visits
summarydata <- data_county %>%
  group_by(fips, year) %>%
  mutate(got_visit = max(traveledonce), 
         tottrips = sum(traveled)) %>%
  distinct(fips, year, .keep_all=TRUE)  %>%
  group_by(year, got_visit) %>%
  mutate(totalpopsum = sum(totalpopulation))  %>%
  group_by(year) %>%
  mutate(uspop = sum(totalpopulation),
         perpop = totalpopsum/uspop)  

mean(summarydata$perpop[summarydata$got_visit==1])
mean(summarydata$tottrips)

#staff
summarydata <- data_county %>%
  group_by(fips, year) %>%
  mutate(got_staff = max(staffeddonce, na.rm=TRUE), 
         totstaff = mean(staff_per, na.rm=TRUE)) %>%
  distinct(fips, year, .keep_all=TRUE)  %>%
  group_by(year, got_staff) %>%
  mutate(totalpopsum = sum(totalpopulation))  %>%
  group_by(year) %>%
  mutate(uspop = sum(totalpopulation),
         perpop = totalpopsum/uspop)  

mean(summarydata$perpop[summarydata$got_staff==1])
mean(summarydata$totstaff[summarydata$got_staff==1])


###########################################
# Analyzing Policy Disagreement: Table 3
##########################################

#load the data
data <- read.dta13("complete_CCES_final.dta",convert.factors=FALSE)
data$race <- as.factor(as.character(data$race))
data <- data[!is.na(data$weight_cumulative),]

# Visits: County Level
data$dist <- data$traveledonce
reg1 <- felm(approval_binary ~ votematchmean2*(dist + copartisan + electionyear + seniority + 
                                                 nokken_poole_dim1 + chair + power +  race + female + age) | member_county + party_year | 0 | icpsr, 
             data = data, weights=data$weight_cumulative)


data$dist <- data$log_traveled
reg2 <- felm(approval_binary ~ votematchmean2*(dist + copartisan + electionyear + seniority + 
                                                 nokken_poole_dim1 + chair + power +  race + female + age) | member_county + party_year | 0 | icpsr, 
             data = data, weights=data$weight_cumulative)


data$dist <- data$traveled
reg3 <- felm(approval_binary ~ votematchmean2*(dist + copartisan + electionyear + seniority + 
                                                 nokken_poole_dim1 + chair + power +  race + female + age) | member_county + party_year | 0 | icpsr, 
             data = data, weights=data$weight_cumulative)


# Visits: MSA Level

data$dist <- data$traveledonce_msa
reg1_msa <- felm(approval_binary ~ votematchmean2*(dist + copartisan + electionyear + seniority + 
                                                     nokken_poole_dim1 + chair + power +  race + female + age) | member_msa + party_year | 0 | icpsr, 
                 data = data, weights=data$weight_cumulative)



data$dist <- data$log_traveled_msa
reg2_msa <- felm(approval_binary ~ votematchmean2*(dist + copartisan + electionyear + seniority + 
                                                     nokken_poole_dim1 + chair + power +  race + female + age) | member_msa + party_year | 0 | icpsr, 
                 data = data, weights=data$weight_cumulative)


data$dist <- data$traveled_msa
reg3_msa <- felm(approval_binary ~ votematchmean2*(dist + copartisan + electionyear + seniority + 
                                                     nokken_poole_dim1 + chair + power +  race + female + age) | member_msa + party_year | 0 | icpsr, 
                 data = data, weights=data$weight_cumulative)

# Staff: County Level
data$dist <- data$staffeddonce
reg4 <- felm(approval_binary ~ votematchmean2*(dist + copartisan + electionyear + seniority + 
                                                 nokken_poole_dim1 + chair + power +  race + female + age) | member_county + party_year | 0 | icpsr, 
             data = data, weights=data$weight_cumulative)


data$dist <- data$log_staffed
reg5 <- felm(approval_binary ~ votematchmean2*(dist + copartisan + electionyear + seniority + 
                                                 nokken_poole_dim1 + chair + power +  race + female + age) | member_county + party_year | 0 | icpsr, 
             data = data, weights=data$weight_cumulative)


data$dist <- data$staff_per
reg6 <- felm(approval_binary ~ votematchmean2*(dist + copartisan + electionyear + seniority + 
                                                 nokken_poole_dim1 + chair + power +  race + female + age) | member_county + party_year | 0 | icpsr, 
             data = data, weights=data$weight_cumulative)


# Staff: MSA Level
data$dist <- data$staffeddonce_msa
reg4_msa <- felm(approval_binary ~ votematchmean2*(dist + copartisan + electionyear + seniority + 
                                                     nokken_poole_dim1 + chair + power +  race + female + age) | member_msa + party_year | 0 | icpsr, 
                 data = data, weights=data$weight_cumulative)


data$dist <- data$log_staffed_msa
reg5_msa <- felm(approval_binary ~ votematchmean2*(dist + copartisan + electionyear + seniority + 
                                                     nokken_poole_dim1 + chair + power +  race + female + age) | member_msa + party_year | 0 | icpsr, 
                 data = data, weights=data$weight_cumulative)


data$dist <- data$staff_per_msa
reg6_msa <- felm(approval_binary ~ votematchmean2*(dist + copartisan + electionyear + seniority + 
                                                     nokken_poole_dim1 + chair + power +  race + female + age) | member_msa + party_year | 0 | icpsr, 
                 data = data, weights=data$weight_cumulative)


county <- stargazer(reg1, reg3,reg2, reg4, reg6, reg5,
                    omit=c("copartisan", "electionyear", "seniority", "nokken_poole_dim1", "chair", "power",  "female", "race", "age"),
                    add.lines=list(c("Senator-County Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark"),
                                   c("Party-Year Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark")),
                    notes.append = FALSE, notes.label = "",
                    report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                    omit.stat = c("rsq", "ser","f"), font.size = "footnotesize",
                    dep.var.caption  = "Dependent Variable: Approval (0/1)",
                    dep.var.labels = "", model.numbers = FALSE,
                    multicolumn = TRUE,
                    column.labels=c("Any Visits", "Total Visits","ln(Visits+1)","Any Staff", "Pct. Staff","ln(Pct. of Staff + 1)"), align=TRUE,
                    covariate.labels=c("Policy Disagreement", "Local Activity", "Policy Disagreement x Local Activity"),
                    notes="\\parbox[t]{1.05\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with standard errors (clustered on senator) shown in parentheses. The dependent variable is a binary measure of constituent approval. $^*$ indicates $p<0.10$ and $^{**} p<0.05$ (two tailed tests).}",
                    label="linear",
                    digits=3, column.sep.width = "-9pt",
                    digits.extra = 0,
                    title="The Relationship between Local Attentiveness, Policy Representation, and Approval")

msa <- stargazer(reg1_msa, reg3_msa,reg2_msa,reg4_msa, reg6_msa, reg5_msa,
                 omit=c("copartisan", "electionyear", "seniority", "nokken_poole_dim1", "chair", "power",  "female", "race", "age"),
                 add.lines=list(c("Senator-MSA Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark"),
                                c("Party-Year Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark")),
                 notes.append = FALSE, notes.label = "",
                 report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                 omit.stat = c("rsq", "ser","f"), font.size = "footnotesize",
                 dep.var.caption  = "Dependent Variable: Approval (0/1)",
                 dep.var.labels = "", model.numbers = FALSE,
                 multicolumn = TRUE, 
                 column.labels=c("Any Visits", "Total Visits","ln(Visits+1)","Any Staff", "Pct. Staff","ln(Pct. of Staff + 1)"), align=TRUE,
                 covariate.labels=c("Policy Disagreement", "Local Activity", "Policy Disagreement x Local Activity"),
                 notes="\\parbox[t]{1.05\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with standard errors (clustered on senator) shown in parentheses. The dependent variable is a binary measure of constituent approval. $^*$ indicates $p<0.10$ and $^{**} p<0.05$ (two tailed tests).}",
                 label="linear",
                 digits=3, column.sep.width = "-9pt", 
                 digits.extra = 0,
                 title="")
star.panel.out <- star_panel(county, msa,panel.label.fontface="bold",
                             panel.names = c("County", "Metropolitan Statistical Area"), same.summary.stats = FALSE)
cat(star.panel.out, sep = '\n', file = "Table3.tex")

####################################
# Table 4: Vote Choice
####################################

#Vote Choice

data$dist <- data$log_traveled
reg2_vote <- felm(voted_for_inc ~ votematchmean2*(dist + copartisan + nokken_poole_dim1 + chair + power +  race + female + age) | member_county + party_year | 0 | icpsr, 
                  data = data, weights=data$weight_cumulative)

data$dist <- data$log_traveled_msa
reg2_msa_vote <- felm(voted_for_inc ~ votematchmean2*(dist + copartisan  + nokken_poole_dim1 + chair + power +  race + female + age) | member_msa + party_year | 0 | icpsr, 
                      data = data, weights=data$weight_cumulative)

data$dist <- data$log_staffed
reg5_vote <- felm(voted_for_inc ~ votematchmean2*(dist + copartisan + nokken_poole_dim1 + chair + power +  race + female + age) | member_county + party_year | 0 | icpsr, 
                  data = data, weights=data$weight_cumulative)

data$dist <- data$log_staffed_msa
reg5_msa_vote <- felm(voted_for_inc ~ votematchmean2*(dist + copartisan + nokken_poole_dim1 + chair + power +  race + female + age) | member_msa + party_year | 0 | icpsr, 
                      data = data, weights=data$weight_cumulative)



visits_county_vote <- stargazer(reg2_vote, reg5_vote, 
                                omit=c("copartisan", "nokken_poole_dim1", "chair", "power",  "female", "race", "age"),
                                add.lines=list(c("Senator-County Fixed Effects","\\checkmark","\\checkmark"),
                                               c("Party-Year Fixed Effects","\\checkmark","\\checkmark")),
                                notes.append = FALSE, notes.label = "",
                                report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                                omit.stat = c("rsq", "ser","f"), font.size = "footnotesize",
                                dep.var.caption  = "Dependent Variable: Vote for Incumbent (0/1)",
                                dep.var.labels = "", model.numbers = FALSE,
                                multicolumn = TRUE,
                                column.labels=c("ln(Visits + 1)", "ln(Pct. of Staff + 1)"), align=TRUE,
                                covariate.labels=c("Policy Disagreement", "Local Activity", "Policy Disagreement x Local Activity"),
                                notes="\\parbox[t]{.75\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with standard errors (clustered on senator) shown in parentheses. The dependent variable is a binary measure of constituent vote choice. $^*$ indicates $p<0.10$ and $^{**} p<0.05$ (two tailed tests).}",
                                label="linear_vote",
                                digits=3,
                                digits.extra = 0,
                                title="The Relationship between Local Attentiveness, Policy Representation, and Vote Choice")

visits_msa_vote <- stargazer(reg2_msa_vote, reg5_msa_vote,
                             omit=c("copartisan", "nokken_poole_dim1", "chair", "power",  "female", "race", "age"),
                             add.lines=list(c("Senator-MSA Fixed Effects","\\checkmark","\\checkmark"),
                                            c("Party-Year Fixed Effects","\\checkmark","\\checkmark")),
                             notes.append = FALSE, notes.label = "",
                             report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),no.space = TRUE,
                             omit.stat = c("rsq", "ser","f"), font.size = "footnotesize",
                             dep.var.caption  = "Dependent Variable: Vote for Incumbent (0/1)",
                             dep.var.labels ="", model.numbers = FALSE,
                             multicolumn = TRUE, 
                             column.labels=c("ln(Visits + 1)", "ln(Pct. of Staff + 1)"), align=TRUE,
                             covariate.labels=c("Policy Disagreement", "Local Activity", "Policy Disagreement x Local Activity"),
                             notes="\\parbox[t]{.75\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with standard errors (clustered on senator) shown in parentheses. The dependent variable is a binary measure of constituent vote choice. $^*$ indicates $p<0.10$ and $^{**} p<0.05$ (two tailed tests).}",
                             label="linear_vote",
                             digits=3,
                             digits.extra = 0,
                             title="")
star.panel.out <- star_panel(visits_county_vote, visits_msa_vote, panel.label.fontface="bold",
                             panel.names = c("County", "Metropolitan Statistical Area"), same.summary.stats = FALSE)
cat(star.panel.out, sep = '\n', file = "Table4.tex")

